package com.buybuyyi.stylistcrm.user.services;

import com.buybuyyi.stylistcrm.consts.DateFormatPattern;
import com.buybuyyi.stylistcrm.user.dao.RequisitionDao;
import com.buybuyyi.stylistcrm.utils.DateUtil;
import com.buybuyyi.stylistcrm.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import stylistcrm.core.query.ResultPagerModel;
import stylistcrm.core.services.order.dto.OrderByDto;
import stylistcrm.core.services.order.enums.EnumExceptionCode;
import stylistcrm.core.services.order.exception.BizException;
import stylistcrm.core.services.user.EnumUserType;
import stylistcrm.core.services.user.profile.Gender;
import stylistcrm.core.services.user.requisition.QueryRequisitionConditionDto;
import stylistcrm.core.services.user.requisition.Requisition;
import stylistcrm.core.services.user.requisition.RequisitionDto;
import stylistcrm.core.services.user.requisition.EnumRequisitionState;
import stylistcrm.core.services.user.service.AuditingService;
import stylistcrm.core.services.user.service.RequisitionService;
import stylistcrm.core.services.user.service.UserService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by cheny on 2016/10/17.
 */
@Service
public class RequisitionServiceImpl implements RequisitionService {

    /**
     * 申请单数据访问层
     */
    @Autowired
    RequisitionDao requisitionDao;
    /**
     * 用户申请审核服务
     */
    @Autowired
    AuditingService auditingService;
    /**
     * 注册用户申请单
     * @param requisitionDto
     * @return
     * @throws BizException
     */
    @Override
    public Boolean registerRequisition(RequisitionDto requisitionDto) throws BizException {
        if(null==requisitionDto){
            throw new BizException(EnumExceptionCode.EC0001);
        }
        return requisitionDao.insert(requisitionDto);
    }

    /**
     * 获取用户申请审核列表
     * @param queryCondition
     * @return
     * @throws BizException
     */
    @Override
    public ResultPagerModel<RequisitionDto> getRequisitionAuditList(QueryRequisitionConditionDto queryCondition) throws BizException {
        Integer currentPage=queryCondition.getCurrentPage();
        Integer pageSize=queryCondition.getPageSize();
        if(currentPage==null){
            currentPage=1;
            queryCondition.setCurrentPage(currentPage);
        }
        if(pageSize==null){
            pageSize=15;
            queryCondition.setPageSize(pageSize);
        }
        queryCondition.setStart((currentPage-1)*pageSize);
        if(queryCondition.getOrderBy()==null){
            List<OrderByDto> orderByDtos=new ArrayList<>();
            OrderByDto orderByDto=new OrderByDto();
            orderByDto.setOrderName("r.create_time");
            orderByDto.setOrderType(OrderByDto.EnumOrderByType.DESC);
            orderByDtos.add(orderByDto);
            queryCondition.setOrderBy(orderByDtos);
        }
        List<RequisitionDto> requisitionDtoList=requisitionDao.queryByPage(queryCondition);
        ResultPagerModel<RequisitionDto> result=new ResultPagerModel<>();
        result.setCurrentPage(currentPage);
        result.setPageSize(pageSize);
        result.setData(requisitionDtoList);
        Integer totalCount=requisitionDao.queryTotalCount(queryCondition);
        result.setTotalServerItems(totalCount);
        return result;
    }

    /**
     * 获取用户申请详情
     * @param pkId 主键id
     * @return
     * @throws BizException
     */
    @Override
    public RequisitionDto getRequisitionDetail(Long pkId) throws BizException {

        return requisitionDao.queryById(pkId);
    }

    /**
     * 申请单审核
     * @param pkId
     * @return
     * @throws BizException
     */
    @Override
    public Boolean requisitionAudit(Long pkId,Boolean isPassed,String message) throws BizException {
        RequisitionDto requisitionDto=requisitionDao.queryById(pkId);
        return auditingService.audit(requisitionDto,isPassed,message);
    }
}
